import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { AjaxResult } from '@/api/model';
import {
  DriverNode,
  DriverInputDto,
  DriverOutputDto,
  DriverOutputDtoSimple,
  DriverInputDtoSimple,
  DriverPackageInputDto,
  DriverPackageOutputDto,
} from './model';
import { PageRequest, PageData } from '@/utils/common/types';

/** IoT驱动枚举 */
enum Api {
  /** 读取驱动 */
  Read = '/api/iot/admin/driver/read',
  /** 读取驱动简单信息 */
  ReadSimple = '/api/iot/admin/driver/readSimple',
  /** 读取驱动节点 */
  ReadNodes = '/api/iot/admin/driver/readNodes',

  /** 新增驱动 */
  Create = '/api/iot/admin/driver/create',
  /** 更新驱动 */
  Update = '/api/iot/admin/driver/update',
  /** 删除驱动 */
  Delete = '/api/iot/admin/driver/delete',

  /** 更新驱动简单信息 */
  UpdateSimple = '/api/iot/admin/driver/updateSimple',

  /** 获取驱动程序包 */
  GetPackage = '/api/iot/admin/driver/getPackage',
  /** 设置驱动程序包 */
  SetPackage = '/api/iot/admin/driver/setPackage',
}

//#region 读取驱动
/**
 * 读取驱动
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readDriver(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<DriverOutputDto>>(
    {
      url: Api.Read,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取驱动简单信息
/**
 * 读取驱动简单信息
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readDriverSimple(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<DriverOutputDtoSimple>>(
    {
      url: Api.ReadSimple,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取驱动节点
/**
 * 读取驱动节点
 * @param mode 错误消息模型
 */
export function readDriverNodes(mode: ErrorMessageMode = 'message') {
  return httpClient.get<DriverNode[]>(
    {
      url: Api.ReadNodes,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 新增驱动
/**
 * 新增驱动
 * @param dtos 驱动DTO
 * @param mode 错误消息模型
 */
export function createDriver(dtos: DriverInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Create,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 更新驱动
/**
 * 更新驱动
 * @param dtos 驱动DTO
 * @param mode 错误消息模型
 */
export function updateDriver(dtos: DriverInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 删除驱动
/**
 * 删除驱动
 * @param ids 驱动编号
 * @param mode 错误消息模型
 */
export function deleteDriver(ids: (string | bigint)[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Delete,
      params: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 更新驱动简单信息
/**
 * 更新驱动简单信息
 * @param dtos 驱动DTO
 * @param mode 错误消息模型
 */
export function updateDriverSimple(
  dtos: DriverInputDtoSimple[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.UpdateSimple,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 获取驱动程序包
/**
 * 获取驱动程序包
 * @param driverId 驱动编号
 * @param mode 错误消息模型
 */
export function getDriverPackage(driverId: string | bigint, mode: ErrorMessageMode = 'message') {
  return httpClient.get<DriverPackageOutputDto>(
    {
      url: Api.GetPackage,
      params: { driverId: driverId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 设置驱动程序包
/**
 * 设置驱动程序包
 * @param dtos 驱动程序包DTO
 * @param mode 错误消息模型
 */
export function setDriverPackage(
  dtos: DriverPackageInputDto[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SetPackage,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
